defAdmCmd(new AbstractAdmCmdImpl("db"){
	Inputs:"Db.inputs",
	Db:"Db.db",
	execute:function(cmdLine,ctx){
		data=getChannelData(ctx.channel());
		data.put(AdmChannelDataConsts.CurCmd, this);
        data.put(this.Inputs, new StringBuilder());
	},
	executeContinue:function(cmdLine,ctx){
		var data = getChannelData(ctx.channel());
        var inputs = data.get(this.Inputs);
        cmdLine.mark();
        var token = cmdLine.nextToken();
        if ("-q".equals(token)) {
            data.remove(this.Inputs);
            data.remove(this.Db);
            data.remove(AdmChannelDataConsts.CurCmd);
        } else if ("-e".equals(token)) {
            this.executeSql(data, ctx);
        } else if ("-db".equals(token)) {
            this.db(cmdLine, data, ctx);
        } else {
            cmdLine.reset();
            inputs.append(cmdLine.remainString());
        }
	},
	db:function(cmdLine,data,ctx){
		var token = cmdLine.nextToken();
        if (!token) {
            var db = data.get(this.Db);
            ctx.writeAndFlush(db == null //
            ? "had not select db\r\n" //
                    : "cur db is " + db.getName() + "\r\n");
        } else {
            var db = getDb( token);
            if ( !db ) {
                ctx.writeAndFlush("can't find db\r\n");
            } else {
                data.put(this.Db, db);
                ctx.writeAndFlush("cur db is " + db.getName() + "\r\n");
            }
        }
	},
	executeSql:function(data,ctx){
		var db = data.get(this.Db);
        if (!db ) {
            ctx.writeAndFlush("please select db first\r\n");
            return;
        }
        var sql = data.get(this.Inputs).toString();
        try {
            var result = db.execute(sql);
            this.printResult(result, ctx);
        } catch (e) {
            ctx.writeAndFlush(e.getMessage() + "\r\n");
        }
        data.put(this.Inputs, new StringBuilder());
	},
	printResult:function(dataset,ctx){
		if (dataset.isUpdate()) {
            ctx.writeAndFlush("update rows " + dataset.getRowCount() + "\r\n\r\n");
            return;
        }
        ctx.writeAndFlush("result rows " + dataset.getRowCount() + "\r\n\r\n");
        for each(var columnName in dataset.getColumnNames()) {
            ctx.writeAndFlush(columnName + "\t");
        }
        ctx.writeAndFlush("\r\n\r\n");
        for each(var row in dataset.getResults()) {
            for each(var cell in row) {
                ctx.writeAndFlush(cell + "\t");
            }
            ctx.writeAndFlush("\r\n\r\n");
        }
	},
	usage:function(){
		return "-q quit\r\n" //
        + "-e execute query or update\r\n"//
        + "-db show dbName\r\n" //
        + "-db <dbName> select db\r\n";
	}
});
//show tables
